package com.stack.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stack.admin.mapper.AdSensitiveMapper;
import com.stack.admin.service.AdSensitiveService;
import com.stack.common.utils.thread.AdminThreadLocalUtil;
import com.stack.model.admin.dtos.AdSensitiveDto;
import com.stack.model.admin.dtos.PageQueryDto;
import com.stack.model.admin.pojos.AdChannel;
import com.stack.model.admin.pojos.AdSensitive;
import com.stack.model.admin.pojos.AdUser;
import com.stack.model.common.dtos.PageResponseResult;
import com.stack.model.common.dtos.ResponseResult;
import com.stack.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author llz
 * @date 2023/11/14 23:40
 * @description
 */
@Service
@Slf4j
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive>  implements AdSensitiveService {

    @Resource
    private AdSensitiveMapper adSensitiveMapper;


    /**
     * 获取敏感词列表
     *
     * @return
     */
    @Override
    public ResponseResult getSensitiveWordList(PageQueryDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(ObjectUtils.isNotEmpty(dto.getSensitives()), AdSensitive::getSensitives, dto.getSensitives());
        queryWrapper.eq(AdSensitive::getDeleted, "00");
        queryWrapper.orderByDesc(AdSensitive::getCreatedTime);

        IPage<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        IPage<AdSensitive> pageList = adSensitiveMapper.selectPage(page, queryWrapper);
        log.info("数据：{}", pageList.getRecords());
        log.info("总数：{}", pageList.getTotal());

        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageList.getTotal());
        responseResult.setData(pageList.getRecords());
        return responseResult;
    }

    /**
     * 保存敏感词
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveSensitiveWord(AdSensitiveDto dto) {
        if (dto == null || dto.getSensitives() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdUser adUser = AdminThreadLocalUtil.getUser();
        if (adUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        AdSensitive adSensitive = JSON.parseObject(JSON.toJSONString(dto), AdSensitive.class);
        adSensitive.setCreatedTime(new Date());
        adSensitive.setDeleted("00");

        adSensitiveMapper.insert(adSensitive);
        log.info("保存的敏感词id:{}", adSensitive.getId());

        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(adSensitive.getId());
        responseResult.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());

        return responseResult;
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateSensitiveWord(AdSensitiveDto dto) {
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdUser adUser = AdminThreadLocalUtil.getUser();
        if (adUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        LambdaUpdateWrapper<AdSensitive> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(AdSensitive::getId, dto.getId());
        updateWrapper.set(ObjectUtils.isNotEmpty(dto.getSensitives()), AdSensitive::getSensitives, dto.getSensitives());
        adSensitiveMapper.update(null, updateWrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteSensitiveWord(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdUser adUser = AdminThreadLocalUtil.getUser();
        if (adUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        AdSensitive adSensitive = AdSensitive.builder()
                .id(id)
                .deleted("01")
                .build();

        //IService
        updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }
}
